package xx.services.page.rest;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.entity.page.PageLayoutItemContent;
import xx.services.Passport;
import xx.services.page.inter.ILayoutItemContentInterface;
import xx.services.util.Constants;
import xx.util.string.Format;

/**
 * page页面布局列内容管理服务接口
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/page/layout/item/content")
 * @author dengjinye,2011-09-18 $LastChangedDate$ $Author$
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/page/layout/item/content")
public class LayoutItemContentRest extends Passport implements ILayoutItemContentInterface
{

    public LayoutItemContentRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    @PUT
    @Path("/{content_id}/after/{after_id}")
    public String sortAfter(@PathParam("content_id")
    long contentId, @PathParam("after_id")
    long afterId)
    {
        try
        {
            PageLayoutItemContent plic = new PageLayoutItemContent(qr, owner);
            plic.setContentId(contentId).after(afterId);
            return ok(true);
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }

    @PUT
    @Path("/{content_id}/before/{before_id}")
    public String sortBefore(@PathParam("content_id")
    long contentId, @PathParam("before_id")
    long beforeId)
    {
        try
        {
            PageLayoutItemContent plic = new PageLayoutItemContent(qr, owner);
            plic.setContentId(contentId).before(beforeId);
            return ok(true);
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }

    @DELETE
    @Path("/{content_id}")
    public String delete(@PathParam("content_id")
    String contentId)
    {
        try
        {
            if (contentId == null || "".equals(contentId))
                return error("请选择需要删除的内容");
            String[] arr = contentId.split("_");
            for (String id : arr)
            {
                PageLayoutItemContent plic = new PageLayoutItemContent(qr, owner);
                plic.setContentId(Long.parseLong(id)).delete();
            }
            return ok(true);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    @PUT
    @Path("/{content_id}")
    public String update(@PathParam("content_id")
    long contentId, @FormParam("url")
    String url, @FormParam("content")
    String content)
    {
        try
        {
            PageLayoutItemContent plic = new PageLayoutItemContent(qr, owner);
            plic.setContentId(contentId);
            if (!Format.isEmpty(url))
            {
                plic.setUrl(url).update(PageLayoutItemContent.URL);
            }
            else if (!Format.isEmpty(content))
            {
                plic.setContent(content).update(PageLayoutItemContent.CONTENT);
            }

            return ok(true);
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }
}
